#! /usr/bin/perl -w

#   E:\perl_dev\post_hz.pl   
use LWP;
use Encode;
use IO::String;
use Getopt::Long;

#use String;

use HTTP::Headers;
use HTTP::Cookies;
use HTTP::Request;
use HTTP::Response;
use HTTP::Status;


  
# 读写 文件
$wf = 'd:\temp\hz_wf.vcf';
$rf = 'C:\Documents and Settings\Administrator\桌面\火种.txt';
$rf =  encode("gbk", decode("utf8", $rf) );


open($fr,"<".$rf) || die "Couldn't open filename "; 
open($fw,">$wf") || die "Couldn't open filename "; 


    
my $postarg ={
'callCount'=>'1',
'page'=>'/member/t.shtml',
'httpSessionId'=>'CDBC75107BEF0ABBF344898AA25086A2',
'scriptSessionId'=>'6C893CF0E833F8A258C1284884EE8BE5944',
'c0-scriptName'=>'phonebookService',
'c0-methodName'=>'laodEConnection',
'c0-id'=>'0',
'c0-param0'=>'string:70163990',
'batchId'=>'9',
};


my $writevfc='';
my $loopid=10;
# 具体循环 
while($line=<$fr>){
    $loopid++;
    $_ = $line;
    $regfind1 =   /"contactid="(\d+)""/  ;
    $fdId =  $1;
    $regfind2 =   /""===============(.+)$/  ;
    $fdName =  $1;
    #文本文件查找
    if($regfind1  && $regfind2 ){
        #print $fdId . $fdName ,  "\n";  
        $postarg->{'c0-param0'} =  'string:' . $fdId  ;
        $postarg->{'batchId'} =  $loopid ;
        #调取手机号码
        $fdPhone = get_dat($postarg);
        $_ = $fdPhone;
        $regfind3 = /s6\.phoneNumber="(.+?)"/  ;  #s6.phoneNumber=
        if($regfind3){
            $fdPhone = $1;
        }else{
            $fdPhone = $&;
        }
        
 
        print $fdPhone ,'=======', $fdId,'======', $fdName,"\n";
        #die ('test');

        
#简单的数据格式        
$writevfc="BEGIN:VCARD
VERSION:2.1
N;CHARSET=UTF-8:;$fdName;;;
FN;CHARSET=UTF-8:$fdName
TEL;CELL:$fdPhone
END:VCARD
";
        
        $writevfc = encode("gbk", decode("utf8", $writevfc) );
        syswrite($fw,$writevfc  );

    }
    #print $line;
} 

close($fw);  
close($fr);  



#print get_dat($postarg); 




# 换行调用
sub get_dat{
    
    local ($subarg) = @_; 

    my $url = 'http://woshiug.sinaapp.com/get.php';
    $url ='http://www.hozom.com/dwr/call/plaincall/phonebookService.laodEConnection.dwr';
    #print  $postarg;
    my $c = &do_POST( $url,$subarg,)
        or die (     encode("gbk", decode("utf8", "连接失败\n") )   );

    # 返回所有数据 
    #print $c; 
    return $c;
    
}

# 访问接口
sub do_POST {

    my $url = 'http://woshiug.sinaapp.com/get.php';


    my $browser = LWP::UserAgent->new;
    $browser->agent("Mozilla/9.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6");
    
    #$browser->default_header('Cookie'=>$cookie,'Referer'=>$login_url);
    $browser->default_header('jjyy'=>'woshiugaaaa','Referer'=>$url );

    $cookie_jar = HTTP::Cookies->new(file => "d:\\temp\\cookies.dat", autosave => 1);
    $browser->cookie_jar($cookie_jar);
    
    #%cook = ( 'JSESSIONID','C6329F4FDC17FC57866BE8A0E2704762');
    #$browser->default_header('JSESSIONID'=>'C6329F4FDC17FC57866BE8A0E2704762');
    #my $request= new HTTP::Request(GET=>$url);
    #   $request->header('Cookie' => 'JSESSIONID=C6329F4FDC17FC57866BE8A0E2704762');   
       
    #my $resp  = $browser->request($request);
    
    my $resp = $browser->post(@_);
    #print $resp;
    if ($resp->is_success) {
        return $resp->content;   
    }
    else {
        return $resp->status_line;
    }
 
 
}


#   E:\perl_dev\post_hz.pl   


